package org.dingyom.car.controller;

import org.dingyom.basics.util.LoginContext;
import org.dingyom.car.dto.CarOprDto;
import org.dingyom.car.es.doc.CarDoc;
import org.dingyom.car.query.CarSearchQuery;
import org.dingyom.car.service.ICarSearchService;
import org.dingyom.car.service.ICarService;
import org.dingyom.car.domain.Car;
import org.dingyom.car.query.CarQuery;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.PageList;
import org.dingyom.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car){
        //设置店铺名字 和 登陆人员信息 这个以后可能是从前台session中获取 所以不能让web和service联系破坏结构
        car.setShopId(LoginContext.getLoginShop().getId());
        car.setShopname(LoginContext.getLoginShop().getName());
        car.setUserId(LoginContext.getLoginUser().getId());

        try {
            if( car.getId()!=null)
                carService.modify(car);
            else
                carService.add(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Car car = carService.getOne(id);
            return AjaxResult.me().setData(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Car> list = carService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody CarQuery query)
    {
        try {
            PageList<Car> pageList = carService.getPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids)
    {
        try {
            Employee loginUser = LoginContext.getLoginUser();
            CarOprDto carOprDto = new CarOprDto(loginUser.getId(),loginUser.getUsername(),ids);
            carService.onsale(carOprDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上架失败！"+e.getMessage());
        }
    }
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids)
    {
        try {
            Employee loginUser = LoginContext.getLoginUser();
            CarOprDto carOprDto = new CarOprDto(loginUser.getId(),loginUser.getUsername(),ids);
            carService.offsale(carOprDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下架失败！"+e.getMessage());
        }
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            carService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    //===================以上是管理员接口，下面是用户调用的接口=======================//
    @Autowired
    private ICarSearchService carSearchService;
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CarSearchQuery query){
        try {
            PageList<CarDoc> carDoclist = carSearchService.search(query);
            return AjaxResult.me().setData(carDoclist);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
}
